package chatGUI;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import chatControler.ChatControler;
import chatModel.UtilisateurDistant;

/**
 * Classe qui gère l'écran de connexion
 * @author vincent
 */
public class EcranConnexion extends JFrame implements ActionListener, WindowListener {

    protected JLabel lBienvenu;
    protected JLabel lRien;
    protected JLabel lRien1;
    protected JButton bConnect;
    protected JLabel lpseudo;
    protected JTextField textPseudo;
    public EcranduChat ecranChat;
    private ChatControler chatControler;

    /**
     * Constructeur
     * @see chatGUI.EcranConnexion#initComponents() initComponents
     * @param chatControler ChatControler du ChatSystem
     */
    public EcranConnexion(ChatControler chatControler) {
        initComponents();
        addWindowListener(this);
        this.chatControler = chatControler;
    }

    /**
     * Initialise l'ecran
     */
    private void initComponents() {
        this.setTitle("Fenetre de connexion au ChatSystem");

        GridLayout l = new GridLayout(5, 5);
        this.setLayout(l);

        this.setLocation(150, 150);

        this.setSize(100, 100);
        lBienvenu = new JLabel("Bienvenue sur le ChatSystem");
        lRien = new JLabel("");
        lpseudo = new JLabel("Rentrez votre pseudo");
        textPseudo = new JTextField("");
        lRien1 = new JLabel("");
        bConnect = new JButton("Se connecter");

        //Et les ajoute
        this.add(lBienvenu);
        this.add(lRien);
        this.add(lpseudo);
        this.add(textPseudo);
        this.add(lRien1);
        this.add(bConnect);

        //quand on appuie sur entrée ça selecitonner le bouton de connexion
        this.getRootPane().setDefaultButton(bConnect);
        this.pack();
        this.setVisible(true);
        bConnect.addActionListener(this);
    }
    
    /**
     * Méthode redéfinie pour gérer les actions de l'utilisateur sur l'écran de connexion
     * @param e 
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == bConnect) {
            chatControler.getLocalUser().setUserName(textPseudo.getText());
            chatControler.getMsgToGui().getUtilisateurLocal().setUserName(textPseudo.getText());
            chatControler.getMsgToNI().getUtilisateurLocal().setUserName(textPseudo.getText());
            try {
                chatControler.getLocalUser().setMyIp(InetAddress.getLocalHost());
                chatControler.getMsgToGui().getUtilisateurLocal().setMyIp(InetAddress.getLocalHost());
                chatControler.getMsgToNI().getUtilisateurLocal().setMyIp(InetAddress.getLocalHost());
            } catch (UnknownHostException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            UtilisateurDistant allUsers = null;
            try {
                allUsers = new UtilisateurDistant("All Users", InetAddress.getByName("255.255.255.255"));
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            }
            chatControler.getOtherUserList().add(allUsers);
            chatControler.setConnected(true);
        }


    }

    @Override
    public void windowActivated(WindowEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void windowClosed(WindowEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void windowIconified(WindowEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void windowOpened(WindowEvent e) {
        // TODO Auto-generated method stub
    }
}
